package hk.ust.cse.comp3111.studentSchedule;



import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.preference.Preference;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class NavDrawer {
	
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	// nav drawer title
	private CharSequence mDrawerTitle;

	// used to store app title
	private CharSequence mTitle;

	// slide menu items
	private String[] navMenuTitles;
	private TypedArray navMenuIcons;

	private ArrayList<NavDrawerItem> navDrawerItems;
	private NavDrawerListAdapter adapter;
	
	private Activity activity;
	
	public NavDrawer(final Activity a, Bundle b){
		
		activity=a;
		
		mTitle = mDrawerTitle = a.getTitle();
		navMenuTitles = a.getResources().getStringArray(R.array.nav_drawer_items);
		navMenuIcons = a.getResources().obtainTypedArray(R.array.nav_drawer_icons);
		mDrawerLayout = (DrawerLayout) a.findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) a.findViewById(R.id.list_slidermenu);
		
		navDrawerItems = new ArrayList<NavDrawerItem>();
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
		// Import by course Code
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
		// Add events
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
		// View By course
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
		// Group Meeting
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
		// Add events example
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
		// test for DB
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(5, -1)));
		
		navMenuIcons.recycle();
		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
		adapter = new NavDrawerListAdapter(a.getApplicationContext(),navDrawerItems);
		mDrawerList.setAdapter(adapter);
		
		/*a.getActionBar().setDisplayHomeAsUpEnabled(true);
		a.getActionBar().setHomeButtonEnabled(true);
		
		mDrawerToggle = new ActionBarDrawerToggle(a, mDrawerLayout,
				R.drawable.ic_drawer, //nav menu toggle icon
				R.string.app_name, // nav drawer open - description for accessibility
				R.string.app_name // nav drawer close - description for accessibility
		) {
			public void onDrawerClosed(View view) {
				a.getActionBar().setTitle(mTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				a.invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				a.getActionBar().setTitle(mDrawerTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				a.invalidateOptionsMenu();
			}
		};*/
		
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		
		/*if (b == null) {
			// on first time display view for first nav item
			displayView(0);
		}*/
		

	}// end constructor

	
	private class SlideMenuClickListener implements ListView.OnItemClickListener {
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// display view for selected nav drawer item
		displayView(position);
		}
	}
	
	private void displayView(int position) {
		// update the main content by replacing fragments
		Intent intent=null;
		switch (position) {
		case 0:
			//intent = new Intent(activity.getApplicationContext(), ViewCalendar.class);
			intent = new Intent(activity.getApplicationContext(), testActivity.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 1:
			intent = new Intent(activity.getApplicationContext(), ImportByCourse.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 2:
			intent = new Intent(activity.getApplicationContext(), AddEvent.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 3:
			intent = new Intent(activity.getApplicationContext(), ViewCourse.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 4:
			intent = new Intent(activity.getApplicationContext(), GroupMeeting.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 5:
			intent = new Intent(activity.getApplicationContext(), AddExample.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;
		case 6:
			intent = new Intent(activity.getApplicationContext(), dbExample.class);
			intent.putExtra("selected position", position);
			activity.startActivity(intent);
			break;

		default:
			break;
		}
		
/*		mDrawerList.setItemChecked(position, true);
		mDrawerList.setSelection(position);
		setTitle(navMenuTitles[position]);*/


	}
	
	public void setTitle(CharSequence title) {
		mTitle = title;
		activity.getActionBar().setTitle(mTitle);
	}

	public DrawerLayout getmDrawerLayout() {
		return mDrawerLayout;
	}

	public ListView getmDrawerList() {
		return mDrawerList;
	}

	public ActionBarDrawerToggle getmDrawerToggle() {
		return mDrawerToggle;
	}
	
	
	

	
	
	
}//end class
